//Use if using 'properties file'

package dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class AssetsDAO {

	static String assetName = null;
	static int assetID;
	static Properties assetListProperties = new Properties();
	static OutputStream assetList = null;
	static OutputStream newAsset = null;
	static Properties parametersProperties = new Properties();
	static InputStream assetListInput = null;
	static InputStream parametersList = null;

	public static void createfile() {

		try {

			assetListInput = new FileInputStream(
					"F:/Data/AssetTracker/assetList.properties");

			assetListProperties.load(assetListInput);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void closefile() {
		try {
			if (assetList != null)
				assetList.close();
			if (newAsset != null)
				newAsset.close();
			if (parametersList != null)
				parametersList.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static boolean createAssets(String assetName) {

		AssetsDAO.assetName = assetName;

		createfile();

		File f = new File(assetName + ".properties");
		if (f.exists()) {
			System.out.println("Asset already exists");
			return false;
			// get Asset ID and return that !
		}

		else {
			try {
				newAsset = new FileOutputStream("F:/Data/AssetTracker/"
						+ assetName + ".properties");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}

			assetListProperties.setProperty("10003", assetName);

			try {
				assetListProperties.store(newAsset, null);

				System.out.println("Operation completly successfuly!");
				return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
	}

	public static boolean updateList(String assetName) {
		createfile();
		return false;

	}

	public static int getAssetID() {
		try {
			parametersList = new FileInputStream(
					"F:/Data/AssetTracker/parameters.properties");
			parametersProperties.load(parametersList);
			assetID = Integer.parseInt(parametersProperties
					.getProperty("assetCount")) + 1;
			return assetID;
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
	}
}
